package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.easyder.wrapper.base.view.WrapperStatusActivity;
import com.easyder.wrapper.core.model.BaseVo;
import com.easyder.wrapper.utils.LogUtils;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.ImManager;
import net.gyapp.markim.module.basic.Helper;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.AccountChanged;
import net.gyapp.markim.module.basic.event.AccountIml;
import net.gyapp.markim.module.contact.adapter.NewFriendsAdapter;
import net.gyapp.markim.module.contact.vo.NewFrinedsVo;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/31
 * Version: 1.0
 * Desc:
 */


public class NewFriendsActivity extends WrapperStatusActivity<CommonPresenter> {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    NewFriendsAdapter adapter;
    NewFrinedsVo.NewUserFriendsBean temp;

    public static Intent getIntent(Context context) {
        return new Intent(context, NewFriendsActivity.class);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.common_recycler;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        super.initView(savedInstanceState, titleView, intent);
        titleView.setCenterText("新的朋友");
        adapter = new NewFriendsAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                NewFrinedsVo.NewUserFriendsBean item = adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.tv_agree:
                        temp = item;
                        agree(item.userName);
                        break;
                    case R.id.tv_reject:
                        reject(item.userName);
                        break;
                }
            }
        });
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        getNewFriend(false);
        ImManager.getInstance().getAllUser();
    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        super.showContentView(url, dataVo);
        if (url.contains(ApiConfig.API_GET_NEW_USER_FRIENDS)) {
            processNewFriend((NewFrinedsVo) dataVo);
        } else if (url.contains(ApiConfig.API_UPDATE_USER_FRIEND)) {
            ImManager.getInstance().retryAutoLogin();
            getNewFriend(true);
        }
    }


    private void getNewFriend(boolean showProgress) {
        presenter.setNeedDialog(showProgress);
        presenter.postData(ApiConfig.API_GET_NEW_USER_FRIENDS, NewFrinedsVo.class);
    }


    private void processNewFriend(NewFrinedsVo dataVo) {
        if (dataVo.newUserFriends != null) {
            adapter.setNewData(dataVo.newUserFriends);
        }
        if (adapter.getData().size() == 0) {
            adapter.setHeaderView(getHelperView(mRecyclerView, R.layout.layout_status_empty, null));
        }
    }

    int status;

    private void agree(String memberId) {
        status = 1;
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_UPDATE_USER_FRIEND, new RequestParams()
                .put("friendId", memberId)
                .put("status", status)
                .get());
    }

    private void reject(String memberId) {
        presenter.setNeedDialog(true);
        status = 0;
        presenter.postData(ApiConfig.API_UPDATE_USER_FRIEND, new RequestParams()
                .put("friendId", memberId)
                .put("status", status)
                .get());
    }
}
